<?php
require_once '../classes/class.overseaBasicUser.php';
/********************************************************************************* 
 * Power By Oversea , All Right Reserved
 * Class -> Extend from OverseaBasicUser Class
 * For oversea merchant member limited
 * ----------------------------------------------
 * Designer: Cavin Zhang
 * Date: Feb 13, 2012
 * ---------------------------------------------
 * Amendment By:
 * Date:
 ******************************************************/

class overseaMerchantUser extends overseaBasicUser {
	
	private $tableArrayMapping = array('1'=>'org_firststep','2'=>'org_details');
	
	public function __construct($temp_userid)
	{
		parent::__construct ($temp_userid);
	}
	
	protected function usrTableName()
	{
		return $this->tableArrayMapping['2'];
	}
	
	public function getUserEssay(CsDatabaseOpera $db,$limitedNum='')
	{
		//find the total number of the respective catergories
		$tRecorderSet='';
		$tempSum = 0; //used to store the total summary of the essay
		$sql = "SELECT COUNT(*) as total, `chs_name`,`cid` FROM `org_blog` a LEFT JOIN `cs_blog_category` b ON a.blog_classification = b.cid WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."' GROUP BY `blog_classification`";
		$sql .= ($limitedNum == '')? '':' LIMIT 0,'.$limitedNum;
		$storOffiAnnocmt = $db -> executeQuery($sql);
		if ($storOffiAnnocmt)
		{
			foreach ($storOffiAnnocmt as $val)
			{
				$tempSum += (int)$val['total'];
				$tRecorderSet .= '|'.$val['cid'].'_'.base64_encode($val['chs_name']).'_'.$val['total'];	
			}
		}
		//add the total numbers at the end
		$tRecorderSet = $tempSum.$tRecorderSet;
		//errorEventLog::displaysSQLPhase($tRecorderSet);
		return $tRecorderSet;
	}
	
	public function getUserFullListOfEssay(CsDatabaseOpera $db,$limitedNum='')
	{
		$sql = "SELECT `blog_title`,`blog_content`,`date_time`,`bid`,`chs_name` FROM `org_blog` a LEFT JOIN `cs_blog_category` b ON a.blog_classification = b.cid WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."' ORDER BY `bid` DESC";
		$sql .= ($limitedNum == '')? '':' LIMIT 0,'.$limitedNum;
		$tRecordSet = $db -> executeQuery($sql);
		return $tRecordSet;
	}
	
	/*
	 * Get the comments posted against to the user's blog
	 * return number is detemined via the 2nd paramater
	 */
	public function getUserEssayComment(CsDatabaseOpera $db, $type,$num = '')
	{
		//modified by Liang Fan on 2/5/2012, for normal usr should redirct to usr's comment table
		//there is a redundent function in class.overseaBasicUser.php
		if($type == 'share')
		{
			$sql = "SELECT `content`,`blog_id` FROM `cs_kb_comms` WHERE `blog_id` IN (SELECT CONCAT_WS(',',`bid`) FROM `cs_knowledge_base` WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."') 
					ORDER BY `cmid` DESC LIMIT 0, $num";
		}
		else
		{
				if(substr($this -> usrRealID,2,1) == '0')
				{
				$sql = "SELECT `content`,`blog_id` FROM `usr_blog_comments` WHERE `blog_id` IN (SELECT CONCAT_WS(',',`bid`) FROM `usr_blog` WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."') 
				ORDER BY `cmid` DESC LIMIT 0, $num";
				}
				else
				{
					$sql = "SELECT `content`,`blog_id` FROM `cs_blog_comments` WHERE `blog_id` IN (SELECT CONCAT_WS(',',`bid`) FROM `org_blog` WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."') 
					ORDER BY `cmid` DESC LIMIT 0, $num";
				}
			
		}
		$tRecordSet = $db -> executeQuery($sql);
		return $tRecordSet;
	}
	
	public function getUserSMSContactList(CsDatabaseOpera $db, $commuicatorID)
	{
		$sql = "SELECT COUNT(*) as total FROM `cs_msg_box` WHERE (`receiver_id` = '".mysql_real_escape_string($this -> usrRealID)."' AND `sender_id` = '".mysql_real_escape_string($commuicatorID)."') OR 
					(`receiver_id` = '".mysql_real_escape_string($commuicatorID)."' AND `sender_id` = '".mysql_real_escape_string($this -> usrRealID)."')";	
		$tRecordSet = $db -> executeQuery($sql);
		return $tRecordSet;	
	}
	
	public static function getMerchantsType(CsDatabaseOpera $db, $temp_userid)
	{
		$sql = "SELECT b.Sort_Name,a.SecondLevel FROM `cs_classification` a INNER JOIN `cs_sort` b ON b.Sort_ID = a.SecondLevel WHERE `temp_userid`='".mysql_real_escape_string($temp_userid)."'";
		$rs = $db -> executeQuery($sql);
		return $rs;
	}
	
	/**
	 * Desp: Get all or some of the pictures belonged to the merchant
	 * @param CsDatabaseOpera $db
	 * @param unknown_type $limitedNum
	 */
	public function getMerchantsPics(CsDatabaseOpera  $db, $limitedNum='')
	{
		$sql = "SELECT * FROM `cs_photo_magt` WHERE `usr_id` = '".mysql_real_escape_string($this->usrRealID)."' ORDER BY `pid` DESC";
		$sql .= ($limitedNum == '')? '':' LIMIT 0,'.$limitedNum;
		$tRecordSet = $db -> executeQuery($sql);
		return $tRecordSet;
	} 
}

?>